//
//  AddNoticeVC.swift
//  StoreProject
//
//  Created by CYC on 2021/9/30.
//

import UIKit


class AddNoticeVC: BaseViewController, UITextViewDelegate {
    
    var label1 = UILabel()          // 站位
    var textview1 = UITextView()    // 输入
    var picker1 = UIDatePicker()    // 日期
    
    var scrollView = UIScrollView() // 承载
    
    
    // MARK:======================================生命周期========================================
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.titleLabel?.text = "新建日程"
        self.creatUIAction()
        
        // ================监听键盘================
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardShowAction), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardHideAction), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    func creatUIAction() {
        
        
        
        let scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: kNavHeight, width: kScreenWidth, height: kScreenHeight - kNavHeight))
        scrollView.alwaysBounceVertical = true
        scrollView.alwaysBounceHorizontal = false
        scrollView.showsVerticalScrollIndicator = false
        self.view.addSubview(scrollView)
        self.scrollView = scrollView
        
        var height = CGFloat(0.0)
        
        if true {
            
            let view1 = UIView.init(frame: CGRect.init(x: kSpaceSize, y: height + kSpaceSize, width: kScreenWidth - kSpaceSize*2, height: 300))
            view1.backgroundColor = .clear
            view1.c_borderAndRadius(width: kLineSize, color: kLineColor, radius: kRadiusSizeL)
            scrollView.addSubview(view1)
            height = view1.c_bottom()
            
            let textView = UITextView.init(frame: .init(x: kContentSize, y: kContentSize, width: view1.c_width() - kContentSize*2, height: view1.c_height() - kContentSize*2))
            textView.font = kMFont(15)
            textView.textColor = kLabelColor1
            textView.backgroundColor = .clear
            textView.delegate = self
            view1.addSubview(textView)
            self.textview1 = textView
            
            let lab = UILabel.init(frame: CGRect.init(x: 5, y: 5, width: textView.c_width() - 5*2, height: 20))
            lab.textColor = kLabelColor3
            lab.textAlignment = .left
            lab.font = kRFont(15)
            lab.text = "请输入"
            textView.addSubview(lab)
            self.label1 = lab
        }
        
        if true {
            
            let picker = UIDatePicker.init(frame: .init(x: kSpaceSize, y: height + kSpaceSize, width: kScreenWidth - kSpaceSize*2, height: 50))
            scrollView.addSubview(picker)
            height = picker.c_bottom()
            self.picker1 = picker
        }
        
        if true {
            
            let btn = UIButton.init(frame: CGRect.init(x: kSpaceSize, y: height + kSpaceSize, width: kScreenWidth - kSpaceSize*2, height: 50))
            btn.setTitle("确定", for: .normal)
            btn.setTitleColor(.white, for: .normal)
            btn.titleLabel?.font = kMFont(15)
            btn.backgroundColor = kLabelColor1
            btn.c_radius(radius: kRadiusSizeS)
            btn.addTarget(self, action: #selector(button1Action(btn:)), for: .touchUpInside)
            scrollView.addSubview(btn)
            height = btn.c_bottom()
        }
        
        
        if true {
            
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(endEditAction))
            scrollView.addGestureRecognizer(tap)
        }
        
        scrollView.contentSize = CGSize.init(width: CGFloat(kScreenWidth), height: CGFloat(height + 50))
        
    }
    
    
    // MARK:======================================点击响应========================================
    
    // MARK:结束编辑
    @objc func endEditAction() {
        UIApplication.shared.keyWindow?.endEditing(true)
    }
    
    // MARK:确定
    @objc func button1Action(btn: UIButton) {
    
        if self.textview1.text == "" {
            ToastManager.makeToast("请输入")
            self.textview1.becomeFirstResponder()
            return
        }
        
        let calendar = Calendar.current
        let com = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self.picker1.date)
        
        let model = DateNoticeModel(noticeId: DateNoticeModel.getOneNoticeId(),
                                    text: self.textview1.text!,
                                    year: String(com.year!),
                                    month: String(com.month!),
                                    day: String(com.day!),
                                    hour: String(com.hour!),
                                    minute: String(com.minute!),
                                    second: String(com.second!))
        DateNoticeModel.addData(model)
        
        self.navigationController?.popViewController(animated: true)
        
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNoticeChange), object: nil)   // 发送通知
        
        
    }
    
    // MARK:======================================网络请求========================================
    
    
    
    // MARK:======================================代理方法========================================
    
    
    // MARK:======================================通知========================================
    // MARK:键盘弹起
    @objc func keyboardShowAction(notification : Notification) {
        
        let keyboardHeight = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
        self.scrollView.frame = CGRect.init(x: 0, y: kNavHeight, width: kScreenWidth, height: kScreenHeight - kNavHeight - keyboardHeight)
        
    }
    
    // MARK:键盘收起
    @objc func keyboardHideAction() {
        
        self.scrollView.frame = CGRect.init(x: 0, y: kNavHeight, width: kScreenWidth, height: kScreenHeight - kNavHeight)
    }
    
    
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        
        self.label1.isHidden = true
        return true
    }
    
    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
        
        if textView.text.count > 0 {
            self.label1.isHidden = true
        } else {
            self.label1.isHidden = false
        }
        return true
    }
    
    
    
}

